import { All, Controller, Get, Injectable, Query, Req, Res } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, IsNull, Not, Repository } from 'typeorm';
import { Cron, Timeout, Interval } from '@nestjs/schedule';
import { Video } from './video.entity';
import utils from 'src/common/utils';
import { Redis } from 'ioredis';
import { InjectRedis } from '@nestjs-modules/ioredis';
import { apibefore } from 'src/router';
const checkTime = 1000 * 60 * 60 * 5

@Controller(apibefore('/video'))
@Injectable()
export class VideoService {
  constructor(
    @InjectRepository(Video)
    private repository: Repository<Video>,
    @InjectRedis() private readonly redis: Redis,
  ) {}

  @Interval(checkTime)
  async Questions (){
    //视频有出处了，但是提问还没发答案
    let list = await this.repository.query(`
      select distinct video_id from (
        select questions.video_id from questions
        inner join video on video.id = questions.video_id
        where video.ic_group is not null and questions.post_answer is null and video.delete_tag = 0
        order by video.id desc
        limit 0,10
      ) a
    `, [])
    let list2 = await this.repository.query(`
			select distinct video_id from (
        select video.id as video_id from video
        left join questions on questions.video_id = video.id and questions.wx_user = video.wx_user
        where video.original = 0 and video.ic_group is not null and questions.id is null and video.check = 1 and video.delete_tag = 0
        order by video.id desc
        limit 0,10
      ) a
    `,[])
    let txt = []
    if(list.length > 0){
      txt.push('提问视频有出处，未发答案, id：' + list.map(e => e.video_id).join('、') + ';')
    }
    if(list2.length > 0){
      txt.push('视频提问者未发答案,id：' + list2.map(e => e.video_id).join('、'))
    }
    if(txt.length > 0){
      utils.send_to_qw(this.redis, txt.join('\r\n'))
    }
  }

  @Interval(checkTime)
  async Answer(){
    let list = await this.repository.query(`
      select distinct video_id from (
        select video.id as video_id from video
        left join answer on answer.video_id = video.id and answer.check = 1 and answer.delete_tag = 0
        where video.check = 1 and video.ic_group is not null and answer.id is null and video.delete_tag = 0
        order by video.id desc
        limit 0,10
      ) a
    `,[])
    let txt = []
    if(list.length > 0){
      txt.push(`视频已有出处，但没有回答者，id：` + list.map(e => e.video_id).join('、'))
    }
    if(txt.length > 0){
      utils.send_to_qw(this.redis, txt.join('\r\n'))
    }
  }

  @Interval(checkTime)
  async Collection(){
    let list = await this.repository.query(`
      select distinct video_id from (
        select distinct video.id as video_id from video
        inner join wx_like_video on wx_like_video.video_id = video.id
        where video.ic_group is null and wx_like_video.delete_tag = 0 and video.delete_tag = 0
        order by video.id desc
        limit 0,10
      ) a
    `,[])
    let txt = []
    if(list.length > 0){
      txt.push(`收藏视频中有没出处的，id：` + list.map(e => e.video_id).join('、'))
    }
    if(txt.length > 0){
      utils.send_to_qw(this.redis, txt.join('\r\n'))
    }
  }

  @Interval(checkTime)
  async Original(){
    //作品审核通过，但是没有出处
    let list = await this.repository.query(`
			select distinct video_id from (
        select distinct video.id as video_id from video
        where video.check = 1 and video.original = 1 and video.ic_group is null and video.delete_tag = 0
        order by video.id desc
        limit 0,10
      ) a
    `, [])
    let txt = []
    if(list.length > 0){
      txt.push(`作品没有出处，id：` + list.map(e => e.video_id).join('、'))
    }
    if(txt.length > 0){
      utils.send_to_qw(this.redis, txt.join('\r\n'))
    }

    //认证用户，有非认证作品
    let list2 = await this.repository.query(`
			select distinct video_id from (
        select distinct video.id as video_id from video
        inner join user_setting on user_setting.wx_user = video.wx_user
        where user_setting.key = 'overseas' and user_setting.value = 1 and video.original = 1 and video.check = 1 and video.overseas != 1 and video.source = 0
        order by video.id desc
        limit 0,10
      ) a
    `,[])
    if(list2.length > 0){
      txt = []
      txt.push(`认证用户，有非认证作品，id：` + list.map(e => e.video_id).join('、'))
    }
    if(txt.length > 0){
      utils.send_to_qw(this.redis, txt.join('\r\n'))
    }
  }

}
